/**
 * @description 存款持仓
 * @author wangjianjun
 */

import React, { PureComponent } from 'react';
import { TableC, ExportC } from '../../../../comps';
import { QuestionCircleOutlined } from '@ant-design/icons';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Row, Col, Tooltip } from 'antd';
import { connect } from 'dva';
import TotalAccount from '../common/TotalAccount';
import SetColumns from '../common/SetColumns';
import config from '../../../../comps/business/tradingMarket/config';
import _ from 'lodash';
import { findValueByCode } from '../../../../utils/utils';
@Form.create()
@connect(({ positionInquiryM, globalM, loading }) => ({
  positionInquiryM,
  globalM,
  loading: loading.effects['positionInquiryM/depositMoneyTable'],
}))
export default class DepositMoney extends PureComponent {
  constructor(props) {
    super(props);
    this.scrollx = 0;
    this.newColumns = [];
    this.state = {
      search: {},
      columns: [],
      dataSource: [],
      page: 1,
      pageSize: 20,
      total: 0,
      account: {},
      treeData: [],
      checkedKeys: [],
      scrollx: 0,
    };
  }
  componentDidMount() {
    const { dispatch, depositMoneyArgs } = this.props;
    //债券默认查表头信息
    dispatch({
      type: 'positionInquiryM/queryShowCol',
      payload: {
        type: 'Trade_Ana_Deposit_Hold_Info',
      },
      callback: (data) => {},
    });
    this.setState(
      {
        search: depositMoneyArgs,
      },
      () => {
        this.getTableData();
      },
    );
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    const init = !this.props.depositMoneyArgs ? '' : JSON.stringify(this.props.depositMoneyArgs);
    const initN = !nextProps.depositMoneyArgs ? '' : JSON.stringify(nextProps.depositMoneyArgs);

    if (init != initN) {
      this.setState(
        {
          search: nextProps.depositMoneyArgs,
        },
        () => {
          this.getTableData();
        },
      );
    }
  }

  //分页切换
  handleTableChange = (pagination, filter, sorter) => {
    const { current, pageSize } = pagination;
    this.setState(
      {
        page: current,
        pageSize: pageSize,
      },
      () => {
        this.getTableData('pageChange');
      },
    );
  };

  //查询表格数据

  getTableData = (type) => {
    const { dispatch } = this.props;
    const { page, pageSize, search } = this.state;
    const params = {};
    params.pageSize = pageSize;
    if (type != 'pageChange') {
      //点击查询按钮 重置页码
      this.setState({
        page: 1,
      });
      params.page = 1;
    } else {
      //点击页码切换时候调用
      params.page = page;
    }
    //列表数据
    dispatch({
      type: 'positionInquiryM/depositMoneyTable',
      payload: {
        ...params,
        ...search,
      },
      callback: (res) => {
        if (res) {
          res.data &&
            res.data.map((item, key) => {
              item.key = key;
            });
          this.setState({
            dataSource: res.data,
            total: res.total,
          });
        } else {
          this.setState({
            dataSource: [],
            total: 0,
          });
        }
      },
    });

    if (type != 'pageChange') {
      //获取统计信息
      dispatch({
        type: 'positionInquiryM/getSearchTotal',
        payload: {
          ...search,
          type: '4',
        },
        callback: (data) => {
          this.setState({
            account: data,
          });
        },
      });
    }
  };

  // 导出参数调整
  changeSearch() {
    const { search } = this.state;
    let newSearch = _.cloneDeep(search);
    if (newSearch && newSearch.fundId) {
      newSearch.fundId = newSearch.fundId.join(',');
    }
    return newSearch;
  }

  render() {
    const {
      positionInquiryM: { columnsInfo, checkedKeysInfo },
      loading,
    } = this.props;
    const {
      globalM: {
        dataDictionary: { CKLX = [], JXJC = [] },
      },
    } = this.props;
    const { search } = this.state;
    const columns = _.cloneDeep(columnsInfo[4].columns);
    this.newColumns = columns.filter((item) => item.display == true);
    if (this.newColumns && this.newColumns.length > 10) {
      const a = this.newColumns.length;
      this.scrollx = a * 150;
    } else {
      this.scrollx = 0;
    }

    this.newColumns.map((item) => {
      if (item.key == 'depositPeriodType') {
        item.render = (text) => {
          //字典翻译 存款类型
          return findValueByCode(text, CKLX);
        };
      }
      if (item.key == 'id.dataDt' || item.key == 'enddate' || item.key == 'begindate') {
        item.render = (text) => {
          if (text != '--' && text) {
            return text.slice(0, 4) + '-' + text.slice(4, 6) + '-' + text.slice(6);
          } else {
            return '--';
          }
        };
      }
      if (item.key == 'depositTerm' || item.key == 'enFeeRatio' || item.key == 'interestBasis') {
        item.align = 'right';
      }

      if (item.key == 'enBalance' || item.key == 'termBalance' || item.key == 'enInterest') {
        item.type = 'money';
      }
      if (item.key == 'investmentType') {
        item.title = (
          <span>
            投资类型
            <Tooltip title='展示的投资类型为同组合同标的的最近一次修改持仓操作'>
              <QuestionCircleOutlined style={{ paddingLeft: '7px' }} />
            </Tooltip>
          </span>
        );
      }
      if (item.key == 'interestBasis') {
        item.render = (text, record) => {
          return findValueByCode(text, JXJC) || '--';
        };
      }
      if (item.key == 'id.dataDt') {
        item.render = (_, record) => {
          const text = record?.id?.dataDt;
          if (text != '--' && text) {
            return text.slice(0, 4) + '-' + text.slice(4, 6) + '-' + text.slice(6);
          } else {
            return '--';
          }
        };
      }
      if (item.key == 'jysc') {
        item.render = (text, record) => {
          if (record?.id?.exchangecode) {
            let findvalue = config.filter((item) => {
              return item.value == record.id.exchangecode;
            });
            if (findvalue && findvalue.length > 0) {
              return findvalue[0].title;
            } else {
              return '--';
            }
          } else {
            return '--';
          }
        };
      }
    });

    return (
      <div>
        <div style={{ display: 'flex' }}>
          <div style={{ flex: 1 }}>
            <TotalAccount account={this.state.account} type='0' />
          </div>
          <div style={{ display: 'flex' }}>
            <div>
              <ExportC style={{ marginTop: '10px' }} url='api/v1/tradeAnaDepositHoldInfo/exportDeposit' search={this.changeSearch()} />
            </div>
            <div style={{ marginLeft: '5px', marginTop: '10px' }}>
              <SetColumns type='4' parameter='Trade_Ana_Deposit_Hold_Info' checkedKeysInfo={checkedKeysInfo} />
            </div>
          </div>
        </div>
        <TableC
          columns={this.newColumns || []}
          dataSource={this.state.dataSource || []}
          onChange={this.handleTableChange}
          current={this.state.page}
          pageSize={this.state.pageSize}
          total={this.state.total}
          selfScrollY={true}
          scroll={{ x: this.scrollx }}
          loading={loading}
          rowKey={'key'}
        />
      </div>
    );
  }
}
